package nc.bs.mmpac.sumpickm.rule;

import java.util.HashSet;
import java.util.Set;

import nc.bs.mmpac.sumpickm.adapter.PubPACAdapter;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.pubitf.mmpac.pickm.sumpickm.IPickmBizServiceForSumPickm;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.mmpac.pickm.param.PickmRewriteForSumPickmParam;
import nc.vo.mmpac.sumpickm.entity.AggSumPickmVO;
import nc.vo.mmpac.sumpickm.entity.SumPickmBodyVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;

public class SumPickmRewritePickmWhenInsertRule implements IRule<AggSumPickmVO> {

    @Override
    public void process(AggSumPickmVO[] vos) {

        if (MMValueCheck.isEmpty(vos)) {
            return;
        }
        Set<PickmRewriteForSumPickmParam> paramSet = new HashSet<PickmRewriteForSumPickmParam>();
        PickmRewriteForSumPickmParam param = null;

        for (AggSumPickmVO vo : vos) {
            SumPickmBodyVO[] bodys = (SumPickmBodyVO[]) vo.getChildren(SumPickmBodyVO.class);
            if (MMValueCheck.isEmpty(bodys)) {
                continue;
            }
            for (SumPickmBodyVO body : bodys) {
                param = new PickmRewriteForSumPickmParam();
                param.setBsumpickm(UFBoolean.TRUE);
                param.setCheadid(body.getCsourcebillid());
                param.setCbodyid(body.getCsourcebillbid());
                param.setTs(body.getTssrc());
                paramSet.add(param) ;
            }
        }

        if (!MMValueCheck.isEmpty(paramSet)) {
            try {
                IPickmBizServiceForSumPickm service = PubPACAdapter.getIPickmBizServiceForSumPickm();
                service.rewriteBsumPickm(paramSet.toArray(new PickmRewriteForSumPickmParam[0]));
            }
            catch (BusinessException e) {
                ExceptionUtils.wrappException(e);
            }
        }

    }

}
